
public class TriangularLeftStepMembershipFunction extends MembershipFunction {

	public TriangularLeftStepMembershipFunction(double mean, double width) {
		lowerBound = mean - width;
		sampledValues = new double[(int)(width * 2 / RESOLUTION) + 1];
		for (int i = 0; i < sampledValues.length; i++) {
			double x = lowerBound + i * RESOLUTION;
			if (x > mean)
				sampledValues[i] = Math.max(Math.min((x - lowerBound) / width, ((mean + width) - x) / width), 0);
			else
				sampledValues[i] = 1;
		}
	}

	public double getValue(double x) {
		int index = (int)((x - lowerBound) / RESOLUTION);
		if (index >= 0 && index < sampledValues.length && x >= lowerBound)
			return sampledValues[index];
		else if (x < lowerBound) 
			return 1;
		return 0;
	}
}
